package com.banggood.pss.mqtools.config;

import com.banggood.pss.mqtools.util.ChannelCreator;
import com.banggood.pss.mqtools.util.ConnectionUtil;
import com.rabbitmq.client.ConnectionFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author Mr.chen
 * @date 2020/8/18 14:23
 */
@Configuration
@ConfigurationProperties(prefix = "spring.rabbitmq")
public class RabbitMqConfig {

    protected static final Log logger = LogFactory.getLog(RabbitMqConfig.class);

    String host;

    int port;

    String username;

    String password;

    String virtualHost;

    @Bean
    public ConnectionFactory rabbitMqConnectionFactory(){
        logger.info("参数: " + toString());
        return ConnectionUtil.getConnectionFactory(host, port, virtualHost, username, password);
    }

    @Bean
    public ChannelCreator channelCreator() throws IOException, TimeoutException {
        ConnectionFactory cf = rabbitMqConnectionFactory();
        return new ChannelCreator(cf.newConnection());
    }

    public String getVirtualHost() {
        return virtualHost;
    }

    public void setVirtualHost(String virtualHost) {
        this.virtualHost = virtualHost;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "RabbitMqConfig{" +
                "host='" + host + '\'' +
                ", port=" + port +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", virtualHost='" + virtualHost + '\'' +
                '}';
    }
}

